package edu.rit.drh4818.raytracing.util;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

import edu.rit.drh4818.raytracing.light.ColorMixer;

/**
 * Class to methodize super sampling.
 * 
 * @author Darren Headrick
 *
 */
public class SuperSampling 
{
	/**
	 * Get the color using supersampling.
	 * @param source Source ray to sample from.
	 * @param t T value to of ray at offset.
	 * @param off Offset to be added in each dimension, at T.
	 * @param depth Depth of the getColor
	 * @return Sum of all colors.
	 */
	public static Color getColor( Ray source, int t, int off, int depth )
	{
		Color sum = Color.black;
		List<Ray> rays = getRays( source, t, off);
		
		for( Ray ray : rays )
		{
			Color hit = ray.findNearestIntersection().getColor( depth );
			sum = ColorMixer.add( 
					sum, ColorMixer.multiply(hit, 1F/rays.size() ) );
		}
		
		return sum;
	}
	
	/**
	 * Get the a sampling of rays.
	 * @param source Source ray.
	 * @param t T value of offset.
	 * @param off Amount to offset.
	 * @return List of all rays.
	 */
	private static List<Ray> getRays( Ray source, int t, int off )
	{
		List<Ray> rays = new ArrayList<Ray>();
		rays.add( source );
		
		double x1 = source.getX( t ), y1 = source.getY( t ), z1 = source.getZ( t );
		
		rays.add( 
				Ray.rayFromPoints(
						source.x0, source.y0, source.z0, 
						x1 + off, y1, z1 ) );
		rays.add( 
				Ray.rayFromPoints(
						source.x0, source.y0, source.z0, 
						x1 - off, y1, z1 ) );
	
	
		rays.add( 
				Ray.rayFromPoints(
						source.x0, source.y0, source.z0, 
						x1, y1 + off, z1 ) );
		rays.add( 
				Ray.rayFromPoints(
						source.x0, source.y0, source.z0, 
						x1, y1 - off, z1 ) );
	
		rays.add( 
				Ray.rayFromPoints(
						source.x0, source.y0, source.z0, 
						x1, y1, z1 + off  ) );
		rays.add( 
				Ray.rayFromPoints(
						source.x0, source.y0, source.z0, 
						x1, y1, z1 - off ) );
	
		
		return rays;
	}
}
